package de.unimannheim.ki.tests;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import de.unimannheim.ki.databaseentities.Musicuser;
import de.unimannheim.ki.usermanagement.DatabaseUserDAO;

public class SimplePersistenceTest {

	 private static final String PERSISTENCE_UNIT_NAME = "my-music-search";
	 private static EntityManagerFactory factory;
	 
	/**
	 * @param args
	 */
	 public static void main(String[] args) {
		 
//		DatabaseUserDAO userDao = DatabaseUserDAO.getDAO();
		
	    
//		System.out.println(userDao.getUser("10").getFirstname());
		
		    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		    EntityManager em = factory.createEntityManager();
		    
		    // Create new user
		    em.getTransaction().begin();
		    Musicuser user = new Musicuser();
		    user.setFbid("99");
		    user.setFirstname("Dude");
		    user.setName("Dede");
		    em.persist(user);
		    em.getTransaction().commit();
		    
		    // Read the existing entries and write to console
		    Query q = em.createQuery("select t from Musicuser t");
		    List<Musicuser> userList = q.getResultList();
		    for (Musicuser tmp : userList) {
		      System.out.println(tmp.getName() + tmp.getLastfmid());
		    }
		    System.out.println("Size: " + userList.size());

		    // Create new user

		 // Read the existing entries and write to console
		    userList = q.getResultList();
		    for (Musicuser tmp : userList) {
		      System.out.println(tmp.getLastfmid() + tmp.getFirstname());
		    }
		    System.out.println("Size: " + userList.size());

		    em.close();
		    
		  }

}
